$(document).ready(function (){
require(['accordion',
		 'tree',
		 'wizard',
		 'monitor',
		 'monitorTool',
		 'timepicker',
		 'inputdrop'] ,function (accordion ,tree ,wizard ,monitor ,monitorTool ,timepicker ,inputdrop){

var monitor_list_url = "deviceMonitor/queryDeviceMonitor";
var _pU_ = {1:"分钟",2:"小时",3:"天"};

var monitor_list_header = [
							  {text:'',name:"t",width:3,hideSearch:"hide"},
							  {text:'健康度',name:"monitorDstatus",width:7,align:"left",render:function(text){
						  		 if (text == 0)
						  		 {
						  			 return '<i class="icon-png icon-png-jk1" style="opacity:1" title="正常"></i>';
						  		 }
						  		 else if (text == 1)
						  		 {
									return '<i class="icon-png icon-png-jk2" style="opacity:1" title="未知"></i>';
						  		 }
						  		 else if (text == 2)
						  		 {
									return '<i class="icon-png icon-png-jk3" style="opacity:1" title="凭证"></i>';
						  		 }
						  		 else if (text == 3)
						  		 {
									return '<i class="icon-png icon-png-jk4" style="opacity:1" title="性能"></i>';
						  		 }
						  		 else
						  		 {
						  		 	return '<i class="icon-png icon-png-jk" style="opacity:1" title="故障"></i>';
						  		 }
							  },searchRender:function (el){
									el.append('<input type="hidden" search-data="treeType" searchCache/>');
									el.append('<input type="hidden" search-data="id" searchCache/>');
									el.append('<input type="hidden" search-data="queryType" value="noQueryComponent" searchCache/>');
									el.append('<input type="hidden" search-data="language" value="1" searchCache/>');
							  }},
							  {text:'',name:"t",width:5,hideSearch:"hide"},
							  {text:'监控器名称',name:"monitorName",width:25,align:"left"},
							  {text:'主IP',name:"asertIP",width:20,align:"left",searchRender:function (el){
							  		index_render_div(el ,{type:"ip",id:"asertIP"});
							  }},
							  {text:'监控器类型',name:"monitorTypeName",width:18,hideSearch:true,align:"left"},
							  {text:'轮询周期',name:"pollDate",align:"left",hideSearch:true,render:function(text,trObj){
												return text+_pU_[trObj.pollUnit]||"";
							  },width:15},
							  {text:'连通',name:"pingStatus",width:7,render:function (text){
							  	 text = text.split(",");
							  	 text = Math.max.apply( Math, text);
						  		 if (text == 1)
						  		 {
						  			 return '<i class="icon-png icon-png-ping" style="opacity:1" title="正常"></i>';
						  		 }
						  		 else if (text == 0)
						  		 {
						  		 	 return '<i class="icon-png icon-png-ping1" style="opacity:1" title="异常"></i>';
						  		 }
						  		 else
						  		 {
						  		 	return '<i class="icon-png icon-png-ping2" style="opacity:1" title="未知"></i>';
						  		 }
							  },hideSearch:true}
						   ];

var monitor_list_oper = [
						    {icon:"rh-icon rh-carry-monitor" ,text:"执行" ,aclick:monitor_excute}
					    ];
var current_treeType = "MONITOR_TYPE_TREE";
var current_treeNode_id = "-1";

view_init();

event_init();

alarm_index();

tree_init()

function view_init()
{
	g_formel.interval_refresh_render($("#refresh_btn") ,{
		elTable : $("#table_div"),
		cbf:function(){
			alarm_index();
		}
	});

	index_list_search_render($("um-search") ,{
		cbf : function (val){
			g_grid.refresh($("#table_div") ,{
				paramObj : {monitorName : val}
			})
		},
		placeholder : "请输入监控器名称"
	})

	$("#monitor_obj_moniotr_tree_div").slimscroll();
}

function event_init()
{
	//模糊查询
	$(".vague-btn").click(function(){
		if (current_treeNode_id != -1) {
			return false
		}
		if($("#vague_div").val() == "")
		{
			g_dialog.operateAlert($("#table_oper"),"请输入关键词。","error");
			return false;
		}
		var paramObj = new Object();
		paramObj.searchFlag = 5;
		paramObj.keyWords = $("#vague_div").val();
		paramObj.queryType = "noQueryComponent";
		paramObj.language = 1;
		paramObj.treeType = current_treeType;
		paramObj.id = "-1";
		monitor_list(paramObj);
	})

	// 查询按钮
	$("#query_btn").click(function (){
		$.ajax({
			type: "GET",
			url: "module/monitor_obj/monitor_tpl.html",
			success :function(data)
			{
				g_dialog.dialog($(data).find("[id=query_template]"),{
					width:"600px",
					height:"430px",
					init:query_dialog_init,
					saveclick:query_dialog_save
				});
			}
		});

		function query_dialog_init(el)
		{
			$("[name=switch]").click(function (){
				if ($(this).val() == "IPV4")
				{
					$("#startIp").attr("disabled" ,false);
					$("#endIp").attr("disabled" ,false);
					$("#mainIp").attr("disabled" ,"disabled");
					$("#mainIp").val("");
					g_validate.clear([$("#mainIp")]);
				}
				else
				{
					$("#startIp").attr("disabled" ,"disabled");
					$("#endIp").attr("disabled" ,"disabled");
					$("#startIp").val("");
					$("#endIp").val("");
					$("#mainIp").attr("disabled" ,false);
					g_validate.clear([$("#startIp"),$("#endIp")]);
				}
			});
		}

		function query_dialog_save(el)
		{

		}
	});

	$("#process_test_btn").click(function (){
		$.ajax({
			type: "GET",
			url: "module/monitor_obj/monitor_tpl.html",
			success :function(data)
			{
				g_dialog.dialog($(data).find("[id=process_test_template]"),{
					width:"500px",
					initAfter:initAfter,
					isDetail : true,
					title : "进程探测"
				});
				function initAfter(el)
				{
					um_ajax_get({
						url : "monitorConfig/doProTest",
						paramObj : {teststore:{connectType:"SSH2"}},
						maskObj : el,
						successCallBack : function (data){
							var data = data.teststore;
							var el_form = el.find("form");
							var serverName;
							for (var i = 0;i < data.length; i++)
							{
								if (data[i].targetType == "server")
								{
									serverName = "应用";
								}
								else if (data[i].targetType == "agent")
								{
									serverName = "代理";
								}
								else if (data[i].targetType == "center")
								{
									serverName = "中心";
								}
								el_form.append('<label class="col-lg-12 control-label tl">探测'+serverName+'服务器进程</label>');
								el_form.append('<label class="col-lg-12 control-label tl">'+serverName+'服务器IP地址：'+data[i].targetIp+'</label>');
								el_form.append('<label class="col-lg-12 control-label tl">'+serverName+'服务器进程状态：'+data[i].serverJarExist+'</label>');
							}
						}
					});
				}
			}
		});
	});

	$("#monitor_obj_monitor_alarm_div div").on("click",function(){
        var  tt = $(this).text();
        if(tt!="0")
        {
        	 var  paramObj={"monitorDstatus":$(this).attr("id")};
        	 monitor_list(paramObj);
        }
       
	});
}

function tree_init(){
	var labelInfoArray = [];
	um_ajax_post({
		url : "deviceMonitor/queryTreeByDeviceType",
		maskObj:"#content_div",
		successCallBack:function (data){
			data = data.reverse()
			data = _.filter(data ,function (obj){
				return (obj.parentID == "-1" || obj.parentID == "root")
			})
			tree.simpleTree($("#accordion") ,{
				zNodes : data,
				zTreeOnClick : function (treeNode){
					current_treeNode_id = treeNode.id;
					monitor_list({
						treeType : current_treeType,
						id : treeNode.id,
						queryType:"noQueryComponent",
						language:"1"
					});
				},
				initIdVal : "-1"
			})
		}
	});
}

function alarm_index()
{
	um_ajax_get({
		url:"monitordisply/TechnicalSupervision/getFaultDistributionByType",
		isLoad:false,
		successCallBack:function(data){
			$("#monitor_obj_monitor_alarm_div").find("div").html("0");
			for (var i = 0; i < data.length; i++) {
				$("[id="+data[i].distributionId+"]").html(data[i].distributionCount);
			}
			
		}
	});
}

function monitor_list(paramObj ,isLoad)
{
	g_grid.render($("#table_div"),{
		 header:monitor_list_header,
		 oper:monitor_list_oper,
		 operWidth:"50px",
		 paramObj:paramObj,
		 url:monitor_list_url,
		 isLoad:isLoad,
		 allowCheckBox:false,
		 hasBorder : false,
		 autoHeight : true,
		 showCount : true,
		 dbClick:function (rowData){
		 	monitor_info(rowData);
		 },
		 dbIndex:3
	});
}

function monitor_info(rowData)
{
	var monitorTypeId = rowData.monitorTypeId;
	if (rowData.version == "LINUX_SNMP")
	{
		monitorTypeId = "LINUX_SNMP";
	}
	var url = "#/monitor_obj/monitor_info?monitorTypeId="
								+monitorTypeId+"&monitorId="+rowData.monitorId+"&monitorName="+rowData.monitorName+"&regionId="+(rowData.regionId==null?"":rowData.regionId)
								+"&assetId="+rowData.asertId+"&hideMenu=1"+"&queryByMonitor=1";
	url = encodeURI(url);
    url = encodeURI(url);
	window.open(url);
}

function monitor_excute(rowData)
{
	um_ajax_get({
		url : "monitorView/doExecute",
		maskObj : $("#table_div"),
		paramObj : {monitorId:rowData.monitorId ,agentId:rowData.nodeId ,monitorType:rowData.monitorTypeId},
		successCallBack :function (data){
			g_dialog.operateAlert();
			g_grid.refresh($("#table_div"));
		}
	});
}


});
});